﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPivotGrid;
using System.Collections.Generic;
using DXApplicationTangche.UC.成品出库单.service;
using System.Data;
using DXApplicationTangche.Utils;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.UC.款式尺寸;
using mendian;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.项目流程.Service;

namespace DXApplicationTangche.report
{
    public partial class XtraReport项目成果物 : DevExpress.XtraReports.UI.XtraReport
    {
        private OrderCommitDTO返回 orderCommitDTO;
        private Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
        private List<DTO尺寸> dTOs尺寸;
        private List<OrderTaskDTO> orderTaskDTO;
        public XtraReport项目成果物(String orderId)
        {
            InitializeComponent();
            this.orderCommitDTO = 订单预览新Service.get订单预览(orderId);
            this.dTOs尺寸 = 款式迭代Service.getSizeListByStyleNameCn(this.orderCommitDTO.styleNameCn.Split()[0]);
            foreach (DTO尺寸 item in this.dTOs尺寸)
            {
                try { item.sizeCd = item.sizeCd.Substring(15); } catch { }
            }
            this.orderTaskDTO = 流程Service.findOrderTaskDTOsByOrderTaskQueryDTO(new OrderTaskQueryDTO(orderId));
            //
            this.BindData();
            //
            this.BindTableData尺寸(this.dTOs尺寸);
            this.DetailReport.DataSource = this.dTOs尺寸;
            //
            this.BindTableData(this.orderCommitDTO.styleAccessoryDTOs);
            this.DetailReport2.DataSource = this.orderCommitDTO.styleAccessoryDTOs;

        }

        /// <summary>
        /// 绑定表头
        /// </summary>
        private void BindData()
        {
            this.xrTable订单号.Text = this.orderCommitDTO.orderNo;
            this.xrTableCell数量.Text = this.orderCommitDTO.orderNumber.ToString();
            this.xrTableCell下单时间.Text = this.orderCommitDTO.orderDate.ToString();
            this.xrTableCell面料号.Text = this.orderCommitDTO.materialCode;
            this.xrTableCell面料名称.Text = this.orderCommitDTO.materialNameCn;
            this.xrTableCell面料成份.Text = this.orderCommitDTO.composition;
            this.xrTableCell销售渠道.Text = this.orderCommitDTO.salesChannel;
            this.xrTableCell销售类别.Text = this.orderCommitDTO.salesType;

            this.xrTableCell项目描述0.Text = this.orderTaskDTO[3].activityDescription;
            this.xrTableCell任务负责人0.Text = this.orderTaskDTO[3].responsor;
            this.xrTableCell执行人0.Text = this.orderTaskDTO[3].assignee;
            this.xrTableCell计划开始时间0.Text = this.orderTaskDTO[3].startTimePlanned.ToString();
            this.xrTableCell实际完成时间0.Text = this.orderTaskDTO[3].actualFinishDate == DateTime.MinValue ? "" : orderTaskDTO[3].actualFinishDate.ToString();

            this.xrTableCell项目描述1.Text = this.orderTaskDTO[0].activityDescription;
            this.xrTableCell任务负责人1.Text = this.orderTaskDTO[0].responsor;
            this.xrTableCell执行人1.Text = this.orderTaskDTO[0].assignee;
            this.xrTableCell计划开始时间1.Text = this.orderTaskDTO[0].startTimePlanned.ToString();
            this.xrTableCell实际完成时间1.Text = this.orderTaskDTO[0].actualFinishDate == DateTime.MinValue ? "" : orderTaskDTO[0].actualFinishDate.ToString();

            this.xrTableCell项目描述2.Text = this.orderTaskDTO[1].activityDescription;
            this.xrTableCell任务负责人2.Text = this.orderTaskDTO[1].responsor;
            this.xrTableCell执行人2.Text = this.orderTaskDTO[1].assignee;
            this.xrTableCell计划开始时间2.Text = this.orderTaskDTO[1].startTimePlanned.ToString();
            this.xrTableCell实际完成时间2.Text = this.orderTaskDTO[1].actualFinishDate == DateTime.MinValue ? "" : orderTaskDTO[1].actualFinishDate.ToString();

            this.xrTableCell项目描述3.Text = this.orderTaskDTO[2].activityDescription;
            this.xrTableCell任务负责人3.Text = this.orderTaskDTO[2].responsor;
            this.xrTableCell执行人3.Text = this.orderTaskDTO[2].assignee;
            this.xrTableCell计划开始时间3.Text = this.orderTaskDTO[2].startTimePlanned.ToString();
            this.xrTableCell实际完成时间3.Text = this.orderTaskDTO[2].actualFinishDate == DateTime.MinValue ? "" : orderTaskDTO[2].actualFinishDate.ToString();

        }

        /// <summary>
        /// 绑定辅料分录
        /// </summary>
        private void BindTableData(List<StyleAccessoryDTO> styleAccessoryDTOs)
        {
            //为XRTable的每一列绑定数据集及对应的字段
            this.xrTableCell辅料类别1.DataBindings.Add("Text", styleAccessoryDTOs, "subMaterialType");//辅料类别
            this.xrTableCell供应商1.DataBindings.Add("Text", styleAccessoryDTOs, "subMaterialSupplier");//供应商
            this.xrTableCell品番1.DataBindings.Add("Text", styleAccessoryDTOs, "subMaterialNumber");//品番
            this.xrTableCell辅料名称1.DataBindings.Add("Text", styleAccessoryDTOs, "subMaterialName");//辅料名称
            this.xrTableCell规格1.DataBindings.Add("Text", styleAccessoryDTOs, "subMaterialSpec");//规格
            this.xrTableCell单耗1.DataBindings.Add("Text", styleAccessoryDTOs, "subMaterialCount");//单耗

        }
        private void BindTableData尺寸(List<DTO尺寸> dTO尺寸s)
        {
            //为XRTable的每一列绑定数据集及对应的字段
            this.xrTableCell位置1.DataBindings.Add("Text", dTO尺寸s, "itemNameCn");//位置
            this.xrTableCell尺码1.DataBindings.Add("Text", dTO尺寸s, "sizeCd");//尺码
            this.xrTableCell尺寸值1.DataBindings.Add("Text", dTO尺寸s, "itemFitValue");//尺寸值
        }






        public DataSet ConvertToDataSet<T>(IList<T> list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }
            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;
            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }
                row = dt.NewRow();
                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];
                    string name = pi.Name;



                    if (dt.Columns[name] == null)
                    {
                        var type = pi.PropertyType;
                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                        {
                            type = type.GetGenericArguments()[0];
                        }
                        column = new DataColumn(name, type);
                        dt.Columns.Add(column);
                    }
                    row[name] = pi.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            return ds;
        }

        
    }
}
